package dhee.wtqshopproject.dao;

import dhee.wtqshopproject.entity.Address;
import dhee.wtqshopproject.entity.Order;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface OrderMapper {
    //查询用户所有订单
    @Select("SELECT * FROM `order` WHERE user_id = #{userId}")
    List<Order> selectByUserId(Integer userId);
    //选择收获地址展示
    @Select("SELECT a.* FROM `order` o JOIN address a ON o.address_id = a.address_id WHERE o.user_id = #{userId}")
    List<Address> selectAddressesByUserId(Integer userId);
    //创建订单 返回订单号
    @Insert("INSERT INTO `order` (order_no, user_id, address_id, total_amount, status, payment_time, create_time) " +
            "VALUES (#{orderNo}, #{userId}, #{addressId}, #{totalAmount}, #{status}, #{paymentTime}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "orderId")
    int createOrder(Order order);
    //删除订单
    @Delete("DELETE FROM `order` WHERE order_id = #{orderId}")
    int deleteOrder(Integer orderId);
// 查询全部订单列表（后台）
    @Select("SELECT * FROM `order`")
    List<Order> selectAllOrders();
    // 订单搜索（按收件人）
    @Select("SELECT o.* FROM `order` o JOIN address a ON o.address_id = a.address_id " +
            "WHERE a.recipient LIKE CONCAT('%', #{recipient}, '%')")
    List<Order> searchOrdersByRecipient(String recipient);
    // 订单状态更新
    @Update("UPDATE `order` SET status = #{status} WHERE order_id = #{orderId}")
    int updateOrderStatus(@Param("orderId") Integer orderId, @Param("status") Integer status);
    // 根据订单ID查询订单详情
    @Select("SELECT * FROM `order` WHERE order_id = #{orderId}")
    Order selectOrderById(Integer orderId);


    //结算 返回订单号和应付金额

    //订单详情  根据订单id查询订单详情 返回订单和订单项

}